package common.properties.editors {
import common.properties.PropertyEditor;

import mx.controls.TextInput;
import mx.core.UIComponent;

/**
 * Main PropertyEditor. Visual editor is TextInput
 * supported dataTypes: int, double, string, text
 */
public class TextPropertyEditor implements PropertyEditor {
    private var _editor:TextInput = new TextInput();
    private var _dataType:String;

    public function TextPropertyEditor(dataType:String = "string") {
        _dataType = dataType;
    }

    public function get component():UIComponent {
        return _editor;
    }

    public function set value(val:Object):void {
        if (val == null) {
            _editor.text = "";
        } else {
            _editor.text = val.toString();
        }
    }

    public function get value():Object {
        switch (_dataType) {
            case "int":
                return parseInt(_editor.text);
            case "double":
                return parseFloat(_editor.text);
            case "string":
                return _editor.text;
            case "text":
                return _editor.text;
        }

        throw new Error("Unsupported dataType: " + _dataType);
    }

    public function registerChangeListener(listener:Function):void {
        _editor.addEventListener("change", listener);
    }
}
}